package model;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanFactoryException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.DuplicateKeyException;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import beans.Bookmark;
import beans.User;

/*
 *  Course: S09-15437 - WEB APPLICATION DEVELOPMENT 
 * 	Name: Jiale Huang
 * 	Andrew ID: jialeh
 * 	Date: 2/23/2009
 *  Assignment: Hw4
 */

public class UserDAOFactoryImpl implements UserDAO {

	private BeanFactory<User> factory;
	
	public UserDAOFactoryImpl() throws DAOException
	{
		try {
			BeanTable<User> table = BeanTable.getInstance(User.class,"jialeh_user");
			if (!table.exists()) {
				table.create("userEmail");   // provide the primary key property(s)
			}
			factory = table.getFactory();
		} catch (BeanFactoryException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}
	
	@Override
	public User create(String userEmail) throws DAOException {
		
		try {
			Transaction.begin();
			User nuser = factory.create(userEmail);
			Transaction.commit();
			return nuser;
			
		} catch (DuplicateKeyException e) {
			Transaction.rollback();
			throw new DAOException("A user with this name already exists.");
		} catch (RollbackException e) {
			throw new DAOException(e);
		}

	}

	@Override
	public User lookup(String userEmail) throws DAOException {

		try {
			return factory.lookup(userEmail);
		} catch (RollbackException e) {
			Transaction.rollback();
			throw new DAOException(e);
		}
	}
	
	@Override
	public void setPassword(User user, String password)
			throws DAOException {

		try {
			Transaction.begin();
			String userEmail = user.getUserEmail();
			User nUser = factory.lookup(userEmail);
			if (nUser == null) {
				throw new DAOException("User name does not exist: "+userEmail);
			}
			nUser.setPassWord(password);
			Transaction.commit();
		} catch (RollbackException e) {
			Transaction.rollback();
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}
	
	public void update(User user) throws DAOException
	{
		
		try{
			
			Transaction.begin();
			User nUser = factory.lookup(user.getUserEmail());
			nUser.setFName(user.getFName());
			nUser.setLName(user.getLName());
			nUser.setHashedPassword(user.getHashedPassword());
			nUser.setSalt(user.getSalt());
			Transaction.commit();
			
		} catch (RollbackException e) {
			Transaction.rollback();
			throw new DAOException(e);			
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}
	
	public List<User> getUsers()throws DAOException
	{
		try {
			User[] temp = factory.match();
			List<User> result;
			if(temp != null){
				result = Arrays.asList(temp);
			}else{
				result = new ArrayList<User>();
			}
			return result;
		} catch (RollbackException e) {
			e.printStackTrace();
			return null;
		}
	}

}
